import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.linear_model import LogisticRegression as LR
from sklearn.preprocessing import StandardScaler

#1.导入数据
flowervalue = load_iris()
X = pd.DataFrame(flowervalue.data,columns=flowervalue.feature_names)
y = flowervalue.target
#2.切分数据集
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,y,test_size=0.3,random_state=420)
print(X)
print(y)
#3.使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化
std = StandardScaler().fit(Xtrain)
Xtrain_ = std.transform(Xtrain)
Xtest_ = std.transform(Xtest)
print("oooo:",Xtrain_)
#4.在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合
p = {
    'C':list(np.linspace(0.05,1,19)),
    'solver':['liblinear','sag','newton-cg','lbfgs']
}

model = LR(penalty='l2',max_iter=10000)

GS = GridSearchCV(model,p,cv=5)
GS.fit(Xtrain_,Ytrain)
best_score = GS.best_score_ #最高的得分：0.9714285714285715
best_params = GS.best_params_#最高参数{'C': 0.41944444444444445, 'solver': 'sag'}
print(best_score,best_params)

#5.将最优的结果重新用来实例化模型，查看训练集和测试集下的分数（20分）(注意多分类需要增加参数  average='micro'
model = LR(penalty='l2',
           max_iter=10000,
           C=GS.best_params_['C'],
           solver=GS.best_params_['solver'])
model.fit(Xtrain_,Ytrain)
scores = model.score(Xtrain_,Ytrain),model.score(Xtest_,Ytest)
print(scores) #(0.9714285714285714, 0.9555555555555556)

#6.计算精准率
scores = cross_val_score(model, Xtrain_, Ytrain, cv=5)
scores2 = cross_val_score(model, Xtest_, Ytest, cv=5)
print('训练集精准率：',np.mean(scores), scores) #0.9714285714285715
print('测试集精准率：',np.mean(scores2), scores2) #0.9333333333333332



